package com.example.Windturbine.Calculation.TreeDOMethod1;

import com.example.Windturbine.DataObject.ProductInfoDO;
import com.example.Windturbine.Error.BusinessException;
import com.example.Windturbine.Error.EmBusinessError;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.poi.ss.formula.functions.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author 丁永新
 * @Date 2021/12/24
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Tree {
    String order;  //当前结点的序号
    ProductInfoDO info; //当前结点的信息对象
    PartTree1 result; //当前结点的LCA计算结果
    HashMap<String,Tree> suntree;//当前结点的子结点map集合

    //构造方法
    public Tree(String order, ProductInfoDO info) {
        this.order = order;
        this.info = info;
        this.suntree = new HashMap<>();
        this.result=null;
    }

    //添加结点方法 构造一颗树
    public void add(Tree root,Tree tree) throws BusinessException {
        String o=tree.getOrder();
        String[] os=o.split("\\.");
        if(os.length==1){
            root.suntree.put(o,tree);
        }else{

                //截取最后一个点前面的order
                String searorder="";
                for(int i=0;i<os.length-1;i++){
                    searorder+=os[i]+".";
                }
                //System.out.println(searorder);
                searorder=searorder.substring(0,searorder.length()-1);
                //System.out.println(searorder);
                Tree tree1=new Tree();
                try{
                    tree1=search(root,searorder);
                }catch (Exception e){
                    throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"搜索出现了错误！");
                }

                if(tree1==null){
                    throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"搜索出现了错误！");
                }

                tree1.suntree.put(o, tree);


        }
    }

    //序号为order的树
    public Tree search(Tree root,String order) throws BusinessException {
        //判断当前结点是不是要找的结点
        if(root.order.equals(order)) return root;
        //不是的话向根节点寻找
        for(Map.Entry<String,Tree> i:root.suntree.entrySet()){
            if(i.getKey().equals(order)){
                return i.getValue();
            }else if(judge(i.getKey(),order)){
                //前缀重合
                return search(i.getValue(),order);
            }
        }
        return null;
    }


    //搜索该order下的子节点
    public List<Tree> searchSon(Tree root,String order) throws BusinessException {
        Tree search = search(root, order);
        List<Tree> list=new ArrayList<>();
        for(Map.Entry<String,Tree> i:search.suntree.entrySet()){
            list.add(i.getValue());
        }
        return list;
    }


    //判断序号s1是否时s2的父序号
    public boolean judge(String s1,String s2) throws BusinessException {
        String[] o1s=s1.split("\\.");
        String[] o2s=s2.split("\\.");
        if(o1s.length>o2s.length) return false;
        try{
            for(int i=0;i<o1s.length;i++){
                if(!o1s[i].equals(o2s[i])) return false;
            }
        }catch (Exception e){
            throw new BusinessException(EmBusinessError.UNKNOWN_ERROR,"比较出现了错误");
        }
        return true;

    }


}
